package ru.st.selenium.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.alertIsPresent;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

import java.util.ArrayList;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.Select;

import ru.st.selenium.model.User;

public class UserManagementPage extends InternalPage {

	public UserManagementPage(PageManager pages) {
		super(pages);
	}

	@FindBy(name = "username")
	private WebElement usernameField;

	@FindBy(name = "email")
	private WebElement emailField;

	@FindBy(name = "password")
	private WebElement passwordField;

	@FindBy(name = "password2")
	private WebElement password2Field;
	
	@FindBy(name = "submit")
	private WebElement saveUserButton;

	private Select permissionDropdown() {
		return new Select(driver.findElement(By.name("permission")));
	}

	public String getUsername() {
		return usernameField.getAttribute("value");
	}

	public UserManagementPage setUsernameField(String text) {
		usernameField.sendKeys(text);
		return this;
	}

	public String getEmail() {
		return emailField.getAttribute("value");
	}

	public UserManagementPage setEmailField(String text) {
		emailField.sendKeys(text);
		return this;
	}

	public UserManagementPage setPassword(String text) {
		passwordField.sendKeys(text);
		return this;
	}

	public UserManagementPage setPassword2(String text) {
		password2Field.sendKeys(text);
		return this;
	}

	public String getRole() {
		return permissionDropdown().getFirstSelectedOption().getText();
	}

	public UserManagementPage setRole(String text) {
		permissionDropdown().selectByVisibleText(text);
		return this;
	}

	public UserManagementPage ensurePageLoaded() {
		super.ensurePageLoaded();
		wait.until(presenceOfElementLocated(By.cssSelector("input#username")));
		wait.until(presenceOfElementLocated(By.cssSelector("div.content h3")));
		return this;
	}

	public UserManagementPage addUser(User user) {
		ensurePageLoaded();
		setUsernameField(user.getLogin());
		setEmailField(user.getEmail());
		setPassword(user.getPassword());
		setPassword2(user.getPassword2());
		saveUserButton.click();
		return this;
	}

	public boolean userExists(User user) {
		List<String> userNames = new ArrayList<String>();
		List<WebElement> userNameElements = driver.findElements(By.xpath("//div/h3[contains(text(),'Users')]/../table/tbody/tr/td[2]/a"));
		for (WebElement userNameElement: userNameElements) {
			String userName = userNameElement.getText().trim();
			userNames.add(userName);
		}
		return (userNames.contains(user.getLogin()));
	}

	public void removeUser(User user) {
		ensurePageLoaded();
		driver.findElement(By.xpath("//div/h3[contains(text(),'Users')]/../table/tbody/tr/td[2]/a[contains(text(),'" + user.getLogin() + "')]/../../td/a/img[contains(@src, 'delete')]")).click();
		wait.until(alertIsPresent()).accept();
	}
}
